<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use think\Db;
use think\Exception;


/**
 * 返回跨域JSON数据
 * @param type $result
 */
function json_api($data){
    return json($data,200,array(
        "Access-Control-Allow-Origin"=>" *",
        "Access-Control-Allow-Headers"=>" Origin, X-Requested-With, Content-Type, Accept",
        "Access-Control-Allow-Methods"=>" GET, POST, PUT",
        /*"Content-Type:application/json"=>" charset=utf-8"*/
    ));
}
function escape($tips = '',$to_type = 'GBK', $former_type= 'UTF-8'){
    if (!empty($tips)){
        $tips = iconv($to_type,$former_type,$tips);
    }
    return $tips;
}
function escape_gbk($tips = '',$to_type = 'UTF-8', $former_type= 'GBK'){
    if (!empty($tips)){
        $tips = iconv($to_type,$former_type,$tips);
    }
    return $tips;
}


function paging(){
    $data['page'] = input('page');
    $data['limit'] = input('limit');
    if(count(array_filter($data)) != count($data)){
        $data['page'] = 1;
        $data['limit'] = 10;
        //return json(['code'=>'5','msg'=>'请传递分页page和limit']);
    }
    return $data;
}


function showPage($count,$page,$limit){

    if(!$page){
        $page=1;
    }
    $nTocalPages=ceil($count/$limit);	//总页数
    if($nTocalPages==0){
        $nTocalPages=1;
    }
    if($page<=0){
        $page=1;
    }
    if($page>$nTocalPages){
        $page=$nTocalPages;
    }
    //echo($page);
    $nStart=($page-1)*$limit;	//起始条数
    return ['start'=>$nStart,'end'=>$limit];
}
//二维数组 值gbk->utf
function characterGbkToUtf8($array){
    header("Content-type:text/html;charset=utf8");
    $data  = [];
    foreach ($array as $key=> $val)
    {
        foreach ($val as $k=> $v)
        {
//            if (mb_check_encoding($v,'UTF-8')){
//                echo $v.'是utf8';
//            }else if (mb_check_encoding ($v,'UTF-8')){
//                echo $v.'是gbk';
//            }else{
//                echo $v.'都不是';
//            }

            $data[$key][$k] = $v;
           // dump($data[$key][$k]);
        }
       // exit;
    }//编码转换
    return $data;

}

function isCharacterType($string){
    $encode = mb_detect_encoding($string, array("ASCII",'UTF-8','GB2312','GBK','BIG5'));
    $tips = false;
    if ($encode == 'EUC-CN'){
        $tips = 'GB2312';
    }else if ($encode == 'UTF-8'){
        $tips = 'UTF-8';
    }else if ($encode == 'GBK'){
        $tips = 'GBK';
    }
    return $tips;
}
/**
 * 将非GBK字符集的编码转为GBK
 *
 * @param mixed $mixed 源数据
 *
 * @return mixed GBK格式数据
 */
function charsetToGBK($mixed)
{
    if (is_array($mixed)) {
        foreach ($mixed as $k => $v) {
            if (is_array($v)) {
                $mixed[$k] = charsetToGBK($v);
            } else {
                $encode = mb_detect_encoding($v, array('ASCII', 'UTF-8','cp936', 'GB2312', 'GBK', 'BIG5'));
                if ($encode == 'UTF-8') {
                    $mixed[$k] = iconv('UTF-8', 'GBK', $v);
                }
            }
        }
    } else {
        $encode = mb_detect_encoding($mixed, array('ASCII','cp936', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
        if ($encode == 'UTF-8') {
            $mixed = iconv('UTF-8', 'GBK', $mixed);
        }
    }
    return $mixed;
}


/**
 * 将非UTF-8字符集的编码转为UTF-8
 *
 * @param mixed $mixed 源数据
 *
 * @return mixed utf-8格式数据
 */
function charsetToUTF8($mixed)
{
    header("Content-type:text/html;charset=utf8");
    if (is_array($mixed)) {
        foreach ($mixed as $k => $v) {
            if (is_array($v)) {
                $mixed[$k] = charsetToUTF8($v);
            } else {
                $encode = mb_detect_encoding($v, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
                if ($encode == 'EUC-CN') {
                    $mixed[$k] = iconv('GBK', 'UTF-8', $v);
                }
            }
        }
    } else {
        $encode = mb_detect_encoding($mixed, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
        if ($encode == 'EUC-CN') {
            $mixed = iconv('GBK', 'UTF-8', $mixed);
        }
    }
    return $mixed;
}
/**
 * @param $arr
 * @param $key
 * @return mixed
 * 二位数组去重
 */
function assoc_title($arr, $key)
{
    $tmp_arr = array();
    foreach ($arr as $k => $v) {
        if (in_array($v[$key], $tmp_arr)) {
            unset($arr[$k]);
        } else {
            $tmp_arr[] = $v[$key];
        }
    }
    return $arr;
}

function isRareWords($current_name){
    try {
        if (json_encode($current_name) === 'null') {
            throw new Exception(escape('生僻字'));
        }
    }catch (Exception $e){
        //echo $e->getMessage();
        $current_name = escape('周堃');
    }
    return $current_name;
}


